import { getSecond, uuid } from '../../utils/common'
import { _sendVerificationCode , _checkVerificationCode } from '../../utils/sendCode'
import { ManageAdmin  , Suggest} from '../../models/'
import { CODE , SEND_TIME , TOKEN_TIME , VERIFICATION_CODE_TYPE , UID_KEY , _getCountLimit , PROGRESS_STATUS} from '../../config/config'
import { _getAccount , _bindPhone } from '../../utils/account' 

/**
 * @api {post} /manage_suggest/suggestList 获取意见建议列表
 * @apiPermission 管理员
 * @apiVersion 1.0.0 
 * @apiName suggestList
 * @apiGroup z_manage_suggest
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/manage_suggest/suggestList'

 * @apiParam {String}  token                   管理员token
 * @apiParam {Integer}  page                   页码 1开始
 * @apiParam {Integer}  limit                  每页显示条数 默认50
 *
 *
 * @apiSuccess {Array}   lit            认证信息集合
 * 
 * @apiSuccess {Object}   suggest            意见对象
 * @apiSuccess {String}   main               意见内容
 * @apiSuccess {String}   qq                 qq
 * @apiSuccess {String}   email              邮箱
 * @apiSuccess {String}   phone              手机
 * @apiSuccess {String}   remark             备注
 * @apiSuccess {String}   ext                拓展字段
 * @apiSuccess {String}   name               姓名
 * @apiSuccess {String}   ctime              创建时间
 * @apiSampleRequest http://copyright.hotchain.vip/api/manage_suggest/suggestList
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 */
export async function suggestList(ctx) {
    let page  	           = ctx.request.body.page
	const status           = ctx.request.body.status
	if(!page) {
		page               = 0
	}
	page                   = Number(page) - 1
	const countLimit       = _getCountLimit(ctx.request.body.limit)
	const where            = {status:1}
	const order			   = [['ctime', 'DESC']]
	const limit			   = [page * countLimit,countLimit]
	const query            = {where,order,limit}
	const suggests         = await Suggest.findAll(query)
	const count            = await Suggest.count({where})

	ctx.body               = CODE.success({
		lit:suggests,count
	})
}


export async function checkToken(ctx,next) {
	const token  = ctx.request.body.token
	const admin  = await ManageAdmin.findOne({where:{uid:token,status:1}})
	if(admin) {
		ctx.body = { admin }
		if(next) {
			return next()
		}
	}
	ctx.body     = CODE.MANAGE_ERROR.ERROR_ADMIN_NOT_FOUND
}
/**
 * @apiDefine ERROR
 * @apiErrorExample {json} 错误返回:
 *     {
 *       "code": "错误码",
 *       "error": "错误描述",
 *       "msg":"错误信息"
 *     }
 */